﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Design;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

namespace ProgNetComponentsDx.Options.Design
{
        class CheckedListBoxSettingsCollectionEditor : CollectionEditor
        {
            public CheckedListBoxSettingsCollectionEditor()
                : base(typeof(CheckedListBoxSettingsCollection))
            {

            }

            CheckedListBoxSettingsCollection collection = null;
            public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
            {
                CheckedListBoxControl control = null;

                if (context != null && context.Instance != null)
                {
                    OptionSettings set = context.Instance as OptionSettings;
                    if (set != null)
                    {
                        if (set.Owner is CheckedListBoxControl)
                            control = set.Owner as CheckedListBoxControl;
                    }
                    else
                    {
                        object own = context.Instance.GetFieldValue("Owner");
                        if (own == null)
                            own = context.Instance.GetPropertyValue("Owner");
                        if (own != null && own is CheckedListBoxControl)
                        {
                            control = own as CheckedListBoxControl;
                        }
                    }
                }
                collection = value as CheckedListBoxSettingsCollection;
                if (collection != null)
                {
                    if (control != null)
                    {
                        List<object> list = new List<object>();

                        bool isNull = false;
                        foreach (CheckedListBoxItem item in control.Items)
                        {
                            if (item.Value != null)
                                list.Add(item.Value);
                            else
                                isNull = true;
                            CheckedListBoxSettings s= collection.GetItem(item.Value);
                            if (s == null)
                            {
                                s = new CheckedListBoxSettings(item.Value, null);
                                collection.Add(s);
                            }
                        }
                        List<CheckedListBoxSettings> strings = new List<CheckedListBoxSettings>();
                        foreach (CheckedListBoxSettings s in collection)
                        {
                            if (s.ItemValue != null)
                            {
                                if (!list.Contains(s.ItemValue))
                                {
                                    strings.Add(s);
                                }
                            }
                        }
                        if (!isNull)
                        {
                            CheckedListBoxSettings s = collection.GetItem(null);
                            if (s != null)
                                collection.Remove(s);
                        }
                        foreach (CheckedListBoxSettings s in strings)
                            collection.Remove(s);
                    }
                }
                object o= base.EditValue(context, provider, value);
                //if (collection != null && control != null)
                //{
                //    foreach (CheckedListBoxSettings s in collection)
                //    {
                //        if (s.StringCode.IsNotNull())
                //        {
                //            foreach (RadioGroupItem item in control.Items)
                //            {
                //                if (item.Value.EqualsSafe(s.ItemValue))
                //                {
                //                    item.Description = ProgNetLanguage.sGetString(s.StringCode);
                //                }
                //            }
                //        }
                //    }
                //}
                return o;
            }

            protected override object CreateInstance(Type itemType)
            {
                object obj =  new CheckedListBoxSettings();
                if (obj != null && collection != null)
                {
                    if (obj is CheckedListBoxSettings)
                    {
                        (obj as CheckedListBoxSettings).Owner = collection.Owner;
                    }
                }
                return obj;
            }

            protected override bool CanRemoveInstance(object value)
            {
                return false;
            }
            protected override CollectionForm CreateCollectionForm()
            {
                CollectionForm f = base.CreateCollectionForm();
                try
                {
                    f.Controls["overArchingTableLayoutPanel"].Controls["addRemoveTableLayoutPanel"].Controls.Clear();
                    f.Controls["overArchingTableLayoutPanel"].Controls.Remove(f.Controls["overArchingTableLayoutPanel"].Controls["downButton"]);
                    f.Controls["overArchingTableLayoutPanel"].Controls.Remove(f.Controls["overArchingTableLayoutPanel"].Controls["upButton"]);
                }
                catch { }
                return f;
            }
        }
}
